<h2>Danh sách sản phẩm</h2>

<table class="employees" border="0">
    <tr>
        <!--<th><input type="checkbox" name="" id="checkAll" onclick="checkAll();" value="" /></th>-->
        <th>ID</th>
        <th>Name</th>
        <th>Action</th>
    </tr>
    <?php
    $cat = new Admin_Models_Category();
    foreach ($cat->getParentCat() as $c) {
        ?>
        <tr class="emp">
            <!--<td class="cell"><input type="checkbox" name="checkOne[]" value="<?php echo $c->getCatId(); ?>" /></td>-->
            <td class="cell" style="text-align: left; padding-left: 10px;"><?php echo $c->getCatId(); ?></td>
            <td class="cell" style="text-align: left;padding-left: 10px;"><b><?php echo $c->getName(); ?></b></td>
            <td class="cell"><a href="<?php echo URL_BASE . "/admin/category?action=edit&name={$c->getName()}&cat_id={$c->getCatId()}&parent_id={$c->getParentId()}#cat"; ?>">Sửa</a>/<a href="<?php echo URL_BASE . '/admin/category/delCat/' . $c->getCatId(); ?>" onclick="if (confirm('Bạn có chắc chắn muốn xóa danh mục này và tất cả các sản phẩm trong đó không?'))
                            return true;
                        else
                            return false;">Xóa</a></td>
        </tr>
        <?php
        foreach ($cat->getChildCat($c->getCatId()) as $cc) {
            ?>
            <tr class="emp">
                <!--<td class="cell"><input type="checkbox" name="checkOne[]" value="<?php echo $cc->getCatId(); ?>" /></td>-->
                <td class="cell" style="text-align: left; padding-left: 10px;"><?php echo $cc->getCatId(); ?></td>
                <td class="cell" style="text-align: left; padding-left: 30px;"><i><?php echo $cc->getName(); ?></i></td>
                <td class="cell"><a href="<?php echo URL_BASE . "/admin/category?action=edit&name={$cc->getName()}&cat_id={$cc->getCatId()}&parent_id={$cc->getParentId()}#cat"; ?>">Sửa</a>/<a href="<?php echo URL_BASE . '/admin/category/delCat/' . $cc->getCatId(); ?>" onclick="if (confirm('Bạn có chắc chắn muốn xóa danh mục này và tất cả các sản phẩm trong đó không?'))
                                    return true;
                                else
                                    return false;">Xóa</a></td>
            </tr>
            <?php
        }
    }
    ?>
</table>

<?php
if (isset($_GET['action'])) {

    $cat_id = $_GET['cat_id'];
    $parent_id = $_GET['parent_id'];
    $action = $_GET['action'];
    $name = $_GET['name'];

    switch ($action) {
        case "edit":
            ?>

            <form name="" action="<?php echo URL_BASE; ?>/admin/category/editCat" method="post" id="cat" class="frm" >
                <p>
                    <label>Tên danh mục:</label>
                    <input type="text" name="txtName" value="<?php echo $name; ?>" />
                    <input type="hidden" name="cat_id" value="<?php echo $cat_id; ?>" />
                </p>
                <p>
                    <label>Danh mục cha:</label>
                    <select name="sltParentId">
                        <option value="0">Danh mục cha</option>
                        <?php
                        foreach ($cat->getParentCat() as $c) {
                            ?>
                            <option value="<?php echo $c->getCatId(); ?>" <?php if ($c->getCatId() == $parent_id) echo 'selected'; ?>><?php echo $c->getName(); ?></option>
                            <?php
                        }
                        ?>
                    </select>
                </p>
                <p>
                    <label></label>
                    <input type="submit" name="btnEdit" value="Sửa" />
                </p>
            </form>

            <?php
            break;

        case "add":
            ?>

            <form name="" action="<?php echo URL_BASE; ?>/admin/category/addCat" method="post" id="cat" class="frm" >
                <p>
                    <label>Tên danh mục:</label>
                    <input type="text" name="txtName" />
                </p>
                <p>
                    <label>Danh mục cha:</label>
                    <select name="sltParentId">
                        <option value="0">Danh mục cha</option>
                        <?php
                        foreach ($cat->getParentCat() as $c) {
                            ?>
                            <option value="<?php echo $c->getCatId(); ?>"><?php echo $c->getName(); ?></option>
                            <?php
                        }
                        ?>
                    </select>
                </p>
                <p>
                    <label></label>
                    <input type="submit" name="btnAdd" value="Thêm" />
                </p>
            </form>

            <?php
            break;

        default:
            break;
    }
}
?>